from num_trans_func import *
from functools import partial

def num_split_str(ret, al_len = 8, point = " ", type = 1):
	out = ""
	if type == 1:
		str_trans = ret[::-1]
	ret = str_trans
	ret_len = len(ret)
	split_n = math.ceil(ret_len/al_len)
	for i in range(0, split_n):
		rev = ret[i*al_len : (i+1)*al_len]
		rev = rev[::-1]
		#print(i, rev)
		if(i != split_n -1):
			out = point + rev + out
		else:
			out = rev + out
	#print(out)
	return out

def str_align(ret, type = 0, al_len = 8):
	len_align = math.ceil(len(ret)/al_len) * al_len
	str_add  = "0"
	#print(ret, len_align, len_add)
	if type == 1:
		str_add = ret[-1]
	out = ret.rjust(len_align, str_add)
	#print(out)
	return out

def str_align64(ret, type = 0):
	str_add  = "0"
	if type == 1:
		str_add = ret[-1]
	out = ret.rjust(64, str_add)
	out = out[0:8] + "_" + out[8:16] + "_" + out[16:24] + "_" + out[24:32] + "_" + out[32:40] + "_" + out[40:48] + "_" + out[48:56] + "_" + out[56:64]
	return out

def str_align32(ret, type = 0):
	str_add  = "0"
	if type == 1:
		str_add = ret[-1]
	out = ret.rjust(32, str_add)
	out = out[0:8] + "_" + out[8:16] + "_" + out[16:24] + "_" + out[24:32]
	return out

def str_align16(ret, type = 0):
	str_add  = "0"
	if type == 1:
		str_add = ret[-1]
	out = ret.rjust(16, str_add)
	out = out[0:8] + "_" + out[8:16] 
	return out

def str_16align4(ret, type = 0):
	str_add  = "0"
	if type == 1:
		str_add = ret[-1]
	out = ret.rjust(16, str_add)
	out = out[0:4] + "_" + out[4:8] + "_" + out[8:12]  + "_" + out[12:16] 
	return out

def str_8align4(ret, type = 0):
	str_add  = "0"
	if type == 1:
		str_add = ret[-1]
	out = ret.rjust(8, str_add)
	out = out[0:4] + "_" + out[4:8]
	return out

def match_to_float(mode, match):
	if mode == 0:#十进制
		return match.group(1)
	elif mode == 1:#十六进制
		return hex_to_dec(str(match.group(1)))
	elif mode == 2:#二进制
		return bin_to_dec(str(match.group(1)))
	elif mode == 3:#补码二进制
		#print("match_to_float.mode3, ", bnr_to_dec(str(match.group(1))))
		return bnr_to_dec(str(match.group(1)))
	elif mode == 4:#FP32
		return hex_to_float(str(match.group(1)))
	elif mode == 5:#FP16
		return hex_fp16_to_float(str(match.group(1)))
	elif mode == 6:#FP64
		return hex_fp64_to_float(str(match.group(1)))
	else:
		return match.group(1)


def main():
	print("misc_func work")
	#value = "110"

	#mode = 3
	#replace_func = partial(match_to_float, mode)

	#tmp = re.sub("(\w+)", replace_func, value)
	#print(tmp)
	#tmp = eval(tmp)
	#print(tmp)

	#split_str("3222211110000", 4, "_")

if __name__ == '__main__':
	main()